package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.constants.Constants;
import com.demo.entity.dto.SessionUserLoginDto;
import com.demo.entity.enums.ApplyStatusEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.DeviceApply;

import com.demo.entity.po.NoticeInfo;
import com.demo.entity.po.SysAccount;
import com.demo.entity.query.DeviceApplyQuery;

import com.demo.entity.query.NoticeInfoQuery;
import com.demo.entity.query.SysAccountQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.NoticeInfoMapper;
import com.demo.mappers.SysAccountMapper;
import com.demo.service.DeviceApplyService;

import com.demo.service.SysAccountService;
import com.demo.utils.EmailUtils;
import com.demo.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.DeviceApplyMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:serviceImpl
 * @Date:2025-03-11
 * @author：author
*/
@Service("deviceApplyService")
public class DeviceApplyServiceImpl implements DeviceApplyService{

	@Resource
	private DeviceApplyMapper<DeviceApply,DeviceApplyQuery> deviceApplyMapper;


	@Resource
	private NoticeInfoMapper<NoticeInfo, NoticeInfoQuery> noticeInfoMapper;


	@Resource
	private EmailUtils emailUtils;

    @Autowired
    private SysAccountMapper<SysAccount, SysAccountQuery> sysAccountMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<DeviceApply> findListByParam(DeviceApplyQuery query) { 
		return this.deviceApplyMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(DeviceApplyQuery query){
		return this.deviceApplyMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<DeviceApply> findListByPage(DeviceApplyQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<DeviceApply> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(DeviceApply bean) { 
		return this.deviceApplyMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<DeviceApply> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceApplyMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<DeviceApply> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceApplyMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据DeviceApplyId查询
	 */
	public DeviceApply getDeviceApplyByDeviceApplyId(Integer deviceApplyId) { 
		return this.deviceApplyMapper.selectByDeviceApplyId(deviceApplyId);
	}

	/**
	 * 根据DeviceApplyId更新
	 */
	public Integer updateDeviceApplyByDeviceApplyId(DeviceApply bean, Integer deviceApplyId) { 
		return this.deviceApplyMapper.updateByDeviceApplyId(bean,deviceApplyId);
	}

	/**
	 * 根据DeviceApplyId删除
	 */
	public Integer deleteDeviceApplyByDeviceApplyId(Integer deviceApplyId) { 
		return this.deviceApplyMapper.deleteByDeviceApplyId(deviceApplyId);
	}


	/**
	 * 保存申请信息
	 * @param bean
	 */
	@Override
	public void saveApply(DeviceApply bean) {
		DeviceApply deviceApplyDB = null;
		if (bean.getDeviceApplyId() != null) {
			deviceApplyDB = this.getDeviceApplyByDeviceApplyId(bean.getDeviceApplyId());
		}
		if (deviceApplyDB != null&&!deviceApplyDB.getUserId().equals(bean.getUserId())) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}

		// 执行 保存逻辑
		if (bean.getDeviceApplyId()==null){
			bean.setStatus(ApplyStatusEnum.WATINGCHECK.getStatus());
			bean.setApplyTime(new Date());
			deviceApplyMapper.insert(bean);
		}else {
			deviceApplyMapper.updateByDeviceApplyId(bean,bean.getDeviceApplyId());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateStatus(Integer applyId, Integer status) {
		ApplyStatusEnum statusEnum = ApplyStatusEnum.getByStatus(status);
		if (statusEnum==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		Date curDate = new Date();
		DeviceApply updateInfo=new DeviceApply();
		updateInfo.setGrantTime(curDate);
		updateInfo.setStatus(statusEnum.getStatus());
		String message="";
		if (statusEnum==ApplyStatusEnum.RETURNED){
			updateInfo.setFinishTime(curDate);
			updateInfo.setReturnTime(curDate);
			message="您已经成功归还器材";
		}else {
			message = String.format("您的申请被 %s 了，请在申请中心查看", statusEnum.getDesc());
		}
		deviceApplyMapper.updateByDeviceApplyId(updateInfo,applyId);
		DeviceApply deviceApplyDB = deviceApplyMapper.selectByDeviceApplyId(applyId);
		// 发送邮箱推送
		SysAccount sysAccountDB=sysAccountMapper.selectByUserId(deviceApplyDB.getUserId());
		if (sysAccountDB!=null&&sysAccountDB.getEmail()!=null){
			emailUtils.sendEmail4Warn(sysAccountDB.getEmail(),message);
		}
		NoticeInfo noticeInfo = new NoticeInfo();
		noticeInfo.setUserId(deviceApplyDB.getUserId());
		noticeInfo.setType(Constants.ONE); // 通知类型设置为1 非公告类型
		noticeInfo.setContent(message);
		noticeInfo.setCreateTime(curDate);
		noticeInfo.setTitle("一条新的通知信息");
		noticeInfo.setUpdateTime(curDate);
		noticeInfo.setPublishTime(curDate);
		noticeInfoMapper.insert(noticeInfo);
	}
}